/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-01-11     sun_shine    first edition
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <oled/oled.h>

/* defined the LED0 pin: PB3 */
#define LED0_PIN    GET_PIN(B, 5)



const char* logo = "\n"
" _____           ____               \n"
"| ____|_ __ ___ | __ )  ___  _   _  \n"
"|  _| | '_ ` _ \\|  _ \\ / _ \\| | | | \n"
"| |___| | | | | | |_) | (_) | |_| | \n"
"|_____|_| |_| |_|____/ \\___/ \\__, | \n"
"                             |___/  \n"
"\n";

const uint8_t bilibili[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xF8,0x00,0x00,0x00,0xF8,0x00,0x00,
0x00,0xF8,0x01,0x00,0x00,0xFC,0x00,0x00,
0x00,0xF8,0x03,0x00,0x00,0xFE,0x00,0x00,
0x00,0xF8,0x07,0x00,0x00,0xFF,0x00,0x00,
0x00,0xF8,0x07,0x00,0x00,0xFF,0x00,0x00,
0x00,0xF0,0x0F,0x00,0x80,0x7F,0x00,0x00,
0x00,0xE0,0x0F,0x00,0x80,0x3F,0x00,0x00,
0x00,0xC0,0x1F,0x00,0xC0,0x1F,0x00,0x00,
0x00,0x80,0x1F,0x00,0xC0,0x0F,0x00,0x00,
0x00,0x00,0x1F,0x00,0xC0,0x07,0x00,0x00,
0x00,0x00,0x3C,0x00,0xE0,0x01,0x00,0x00,
0x00,0x00,0x38,0x00,0xE0,0x00,0x00,0x00,
0x00,0xFE,0xFF,0xFF,0xFF,0xFF,0x0F,0x00,
0x80,0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0x00,
0xE0,0x03,0x00,0x00,0x00,0x00,0x7C,0x00,
0xF0,0x00,0x00,0x00,0x00,0x00,0xF0,0x00,
0x38,0xF8,0xFF,0xFF,0xFF,0xFF,0xC1,0x01,
0x18,0x1E,0x00,0x00,0x00,0x80,0x87,0x01,
0x18,0x07,0x00,0x00,0x00,0x00,0x8E,0x03,
0x08,0x03,0x00,0x00,0x00,0x00,0x1C,0x03,
0x88,0x01,0x00,0x00,0x00,0x00,0x18,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x03,0x0C,0x00,0x10,0x02,
0x88,0x00,0xC0,0x01,0x38,0x00,0x10,0x02,
0x88,0x00,0xF0,0x00,0xF0,0x00,0x10,0x02,
0x88,0x00,0x3C,0x00,0xC0,0x03,0x10,0x02,
0x88,0x00,0x1F,0x00,0x80,0x0F,0x10,0x02,
0x88,0xC0,0x07,0x00,0x00,0x3E,0x10,0x02,
0x88,0xC0,0x03,0x00,0x00,0x3C,0x10,0x02,
0x88,0xC0,0x00,0x00,0x00,0x30,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x10,0x00,0x20,0x00,0x10,0x02,
0x88,0x00,0x30,0x30,0x30,0x00,0x10,0x02,
0x88,0x00,0x60,0xFC,0x18,0x00,0x10,0x02,
0x88,0x00,0xC0,0x87,0x0F,0x00,0x10,0x02,
0x88,0x00,0x80,0x03,0x07,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x00,0x00,0x00,0x00,0x00,0x10,0x02,
0x88,0x01,0x00,0x00,0x00,0x00,0x18,0x02,
0x88,0x03,0x00,0x00,0x00,0x00,0x1C,0x03,
0x08,0x07,0x00,0x00,0x00,0x00,0x0E,0x03,
0x18,0x0E,0x00,0x00,0x00,0x00,0x87,0x01,
0x38,0x7C,0x00,0x00,0x00,0xE0,0xC3,0x01,
0x70,0xE0,0xFF,0xFF,0xFF,0x7F,0xE0,0x01,
0xE0,0x01,0x00,0x00,0x00,0x00,0xF8,0x00,
0xC0,0x07,0x00,0x00,0x00,0x00,0x3E,0x00,
0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0x1F,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00

};


void oled_show_logo(void)
{
    Oled * u8g2 = nullptr;
    u8g2 = Oled::getInstance();
    u8g2->clearBuffer();

    u8g2->drawFrame(0, 0, u8g2->getDisplayWidth(), u8g2->getDisplayHeight());                      // clear the internal memory
    u8g2->drawXBM(2, 2, 60, 60, bilibili);
    u8g2->sendBuffer();
    u8g2->setFont(u8g2_font_bauhaus2015_tr );            // choose a suitable font
    u8g2->drawStr(70, 50, "Emboy");   // write something to the internal memory
    u8g2->setFont(u8g2_font_t0_12b_mr   );
    u8g2->drawStr(70, 20, "bilibili");
    u8g2->sendBuffer();                          // transfer internal memory to the display

}

void logo_show(void)
{
    rt_kprintf(logo);
    oled_show_logo();
}

void console_config(void)
{
    rt_device_t uart = rt_console_get_device();
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;  /* 初始化配置参数 */

    /* step2：修改串口配置参数 */
    config.baud_rate = BAUD_RATE_460800;        //修改波特率为 9600
    config.data_bits = DATA_BITS_8;           //数据位 8
    config.stop_bits = STOP_BITS_1;           //停止位 1
    config.bufsz     = 4096;                   //修改缓冲区 buff size 为 128
    config.parity    = PARITY_NONE;           //无奇偶校验位

    /* step3：控制串口设备。通过控制接口传入命令控制字，与控制参数 */
    rt_device_control(uart, RT_DEVICE_CTRL_CONFIG, &config);

}

int main(void)
{
    int count = 1;
    rt_kprintf(">>>>>>>>>>>>>>>>>>\n");
    // console_config();
    logo_show();

    /* set LED0 pin mode to output */
    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);

    while (count++)
    {
        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED0_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    }

    return RT_EOK;
}

static int ota_app_vtor_reconfig(void)
{
    #define NVIC_VTOR_MASK   0x3FFFFF80
    /* Set the Vector Table base location by user application firmware definition */
    SCB->VTOR = 0x08020000 & NVIC_VTOR_MASK;

    return 0;
}
INIT_BOARD_EXPORT(ota_app_vtor_reconfig);

